﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;

public partial class Manage_System_Email : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();
        ManageHelper.CheckAdminPower("system_configemail");
        if (ManageHelper.PageAct() == "save")
            ConfigSave();
        else if (ManageHelper.PageAct() == "test")
            SendTest();
    }

    private void SendTest()
    {
        string ret = ManageHelper.MailSend(PageRequest.GetString("EmailTest"), KeRui.Kernel.Software.Name + " 的测试邮件。", "这是一封来之 " + KeRui.Kernel.Software.ChineseName + " 重要的测试邮件，发送时间："+DateTime.Now.ToString()+"。");
        //Response.Write(WebConfig.GetString("EmailServer") + " ," +            WebConfig.GetString("EmailAccount") + " ," +           WebConfig.GetString("EmailPassword") + " ," +            WebConfig.GetString("EmailSender") + " ," +            WebConfig.GetInt("EmailPort", 25).ToString());
        //Response.Write("<br>"+ret);
        //Response.End();
        if (ret == "ok")
            Response.Redirect("email.aspx?sucmsg=邮件发送成功！");
        else
            Response.Redirect("email.aspx?errmsg=" + Utils.RemoveHtmlContent(ret));
    }

    private void ConfigSave()
    {
        ManageHelper.CheckAdminPower("system_configemailupdate");
        KeRui.BLL.SysConfig bll = new KeRui.BLL.SysConfig();
        KeRui.Model.SysConfig model = new KeRui.Model.SysConfig();

        model = bll.GetModel("WebConfig", "EmailServer");
        model.Value = PageRequest.GetString("EmailServer");
        bll.Update(model);
        DataCache.RemoveCache("SysConfigValue-WebConfig-EmailServer");

        model = bll.GetModel("WebConfig", "EmailPort");
        model.Value = PageRequest.GetInt("EmailPort",25).ToString();
        bll.Update(model);
        DataCache.RemoveCache("SysConfigValue-WebConfig-EmailPort");

        model = bll.GetModel("WebConfig", "EmailAccount");
        model.Value = PageRequest.GetString("EmailAccount");
        bll.Update(model);
        DataCache.RemoveCache("SysConfigValue-WebConfig-EmailAccount");


        if (PageRequest.GetString("EmailPassword") != "**********")
        {
            model = bll.GetModel("WebConfig", "EmailPassword");
            model.Value = PageRequest.GetString("EmailPassword");
            bll.Update(model);
            DataCache.RemoveCache("SysConfigValue-WebConfig-EmailPassword");
        }

        model = bll.GetModel("WebConfig", "EmailSender");
        model.Value = PageRequest.GetString("EmailSender");
        bll.Update(model);
        ManageHelper.AddLog("system_configemailupdate","修改了邮件设置");
        DataCache.RemoveCache("SysConfigValue-WebConfig-EmailSender");


        Response.Redirect("email.aspx?sucmsg=邮件设置修改成功！");
        
    }
}
